package com.chixing.service.impl;

import com.chixing.mapper.ReviewMapper;
import com.chixing.pojo.*;
import com.chixing.service.MongoDBService;
import com.chixing.util.MongodbUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MongoDBServiceImpl implements MongoDBService {


    public static final String COLLECTIONNAME_REVIEW = "review";

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    private ReviewMapper reviewMapper;



    @Override
    public void Mongo2Mysql() {


        ReviewExample reviewExample = new ReviewExample();
        reviewExample.createCriteria();
        List<Review> reviewList = reviewMapper.selectByExampleWithBLOBs(reviewExample);


        List<BasePojo> basePojoList = mongoTemplate.findAll(BasePojo.class,COLLECTIONNAME_REVIEW);


        System.out.println(basePojoList);

        if(basePojoList.size()>0) {
            List<BasePojo> basePojoList2 = new ArrayList<>();
            for (BasePojo basePojo : basePojoList) {
//                System.out.println(basePojo);
                int count=0;
                for (Review review : reviewList) {
//                    System.out.println(review);
                    System.out.println(review.getMyorderId());
                    System.out.println(basePojo.getOrderId());
                    if (!review.getMyorderId().equals(basePojo.getOrderId())) {
                        count++;
                    }
                }
                if(count>=reviewList.size())
                    basePojoList2.add(basePojo);
            }

            System.out.println("---------------------------------");
            System.out.println("base2:"+basePojoList2);

            if(basePojoList2.size()>0){
                for(BasePojo basePojo :basePojoList2) {
                    System.out.println(basePojo);
                    Review review = new Review();
                    review.setMemberId(basePojo.getMemberId());
                    review.setMyorderId(basePojo.getOrderId());
                    review.setReviewDate(basePojo.getCreateTime());
                    review.setStarShop(basePojo.getScore());
                    review.setReviewContent(basePojo.getWrite());
                    review.setSupportTotal(0);
                    review.setShopId(basePojo.getShopId());
                    review.setCarId(basePojo.getCarId());

                    int rows = reviewMapper.insert(review);
//                if (rows > 0) {
//                    System.out.println("更新一条评论到Mysql");
//                    Query query = Query.query(Criteria.where("orderId").is(basePojo.getOrderId()));
//                    MongodbUtils.remove(query, COLLECTIONNAME_REVIEW);
//                    System.out.println("移除进入到mysql中的数据");
//                }
                    }

                }else{
                    System.out.println("暂无数据需要写入");
                }



            }else {
            System.out.println("mongodb中没有数据");
        }

        }



    }

